﻿using DataModels;
using DataModels.Enumerations;
using DataModels.Models;
using DataModels.Models.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericTools.Models
{
    public class ModelDataRetriever : BaseWebAuthorization
    {
        private static IQServices.IQService webService;

        static ModelDataRetriever()
        {
            webService = new IQServices.IQService();
            webService.AuthenticationHeaderValue = new IQServices.AuthenticationHeader();
            webService.Url = AppConfiguration.WebServiceURL;
        }

        public static List<Report> GetAllExpectedReports()
        {
            switch (AppConfiguration.AccessMethod)
            {
                case DataAccessMethod.DIRECT_ACCESS:
                case DataAccessMethod.OFFLINE_DIRECT_ACCESS:
                    using (var db = new IQSMS_TestDBContext())
                    {
                        db.Configuration.ProxyCreationEnabled = false;
                        var results = from r in db.Reports.Include("ReportDataLocations")
                                      select r;
                        return results.ToList();
                    }

                case DataAccessMethod.OFFLINE_DATA_ACCESS:
                    throw new NotImplementedException("This feature does not support MS Access back-end");

                case DataAccessMethod.WEB_SERVICES:
                    webService.AuthenticationHeaderValue.Username = Username;
                    webService.AuthenticationHeaderValue.PasswordHash = PasswordHash;

                    var result = webService.GetAllExpectedReportsWithDataElements();
                    var output = new List<Report>();
                    foreach (var i in result)
                    {
                        var item = Services.Deserialize(i, typeof(Report)) as Report;
                        output.Add(item);
                    }
                    return output;
            }

            return null;
        }

        public static Report GetExpectedReport(int reportId)
        {
            switch (AppConfiguration.AccessMethod)
            {
                case DataAccessMethod.DIRECT_ACCESS:
                case DataAccessMethod.OFFLINE_DIRECT_ACCESS:
                    using (var db = new IQSMS_TestDBContext())
                    {
                        db.Configuration.ProxyCreationEnabled = false;
                        var results = from r in db.Reports.Include("ReportDataLocations")
                                      where r.RepID == reportId
                                      select r;
                        return results.FirstOrDefault();
                    }

                case DataAccessMethod.OFFLINE_DATA_ACCESS:
                    throw new NotImplementedException("This feature does not support MS Access back-end");

                case DataAccessMethod.WEB_SERVICES:
                    webService.AuthenticationHeaderValue.Username = Username;
                    webService.AuthenticationHeaderValue.PasswordHash = PasswordHash;

                    var result = webService.GetExpectedReportWithDataElements(reportId);
                    var output = Services.Deserialize(result, typeof(Report)) as Report;
                    return output;
            }

            return null;
        }

        #region Instance Methods Wrapping for UI
        public List<Report> GetReports()
        {
            return ModelDataRetriever.GetAllExpectedReports();
        }

        public Report GetReport(int reportId)
        {
            return ModelDataRetriever.GetExpectedReport(reportId);
        }
        #endregion
    }
}
